package oocl.com.WebDataHandle.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import oocl.com.dao.UserDao;
import oocl.com.dao.impl.UserDaoImpl;
import oocl.com.pojo.User;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class WebDataHandleImpl{
	public String createWebPage(){
		UserDao dao = new UserDaoImpl();
		List<User> users = dao.getAllUser();
		String showView = "";
		for(User user:users){
			showView += user.getUname() + " " + user.getSal() + " " + user.getBirth() + " " + user.getSex() + "</br>";
		}
		return showView;
	}
	
	public InputStream loadPicture(HttpServletRequest request) throws IOException{
		InputStream fin = null;
		DiskFileItemFactory factory=new DiskFileItemFactory(20*1024, new File("test"));
		ServletFileUpload upload=new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");//控制中文文件名中的乱码
		upload.setSizeMax(100*1024);
		
		try {
			List<FileItem> items= upload.parseRequest(request);
			for(FileItem item:items){
				if(item.isFormField()){
					System.out.println(item.getFieldName()+"..."+item.getString());
				}
				else{
					System.out.println(item.getName()+"...."+item.getSize());
					fin=item.getInputStream();
				}
			}
			
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		
		return fin;
	}
}
